<?php

	/**
	 * Original data taken from:
	 * ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP866.TXT
	 * @param string $string
	 * @return string
	 */
	function charset_decode_cp866($string)
	{
		$mapping = array(
					"\x80" => "\xD0\x90",
					"\x81" => "\xD0\x91",
					"\x82" => "\xD0\x92",
					"\x83" => "\xD0\x93",
					"\x84" => "\xD0\x94",
					"\x85" => "\xD0\x95",
					"\x86" => "\xD0\x96",
					"\x87" => "\xD0\x97",
					"\x88" => "\xD0\x98",
					"\x89" => "\xD0\x99",
					"\x8A" => "\xD0\x9A",
					"\x8B" => "\xD0\x9B",
					"\x8C" => "\xD0\x9C",
					"\x8D" => "\xD0\x9D",
					"\x8E" => "\xD0\x9E",
					"\x8F" => "\xD0\x9F",
					"\x90" => "\xD0\xA0",
					"\x91" => "\xD0\xA1",
					"\x92" => "\xD0\xA2",
					"\x93" => "\xD0\xA3",
					"\x94" => "\xD0\xA4",
					"\x95" => "\xD0\xA5",
					"\x96" => "\xD0\xA6",
					"\x97" => "\xD0\xA7",
					"\x98" => "\xD0\xA8",
					"\x99" => "\xD0\xA9",
					"\x9A" => "\xD0\xAA",
					"\x9B" => "\xD0\xAB",
					"\x9C" => "\xD0\xAC",
					"\x9D" => "\xD0\xAD",
					"\x9E" => "\xD0\xAE",
					"\x9F" => "\xD0\xAF",
					"\xA0" => "\xD0\xB0",
					"\xA1" => "\xD0\xB1",
					"\xA2" => "\xD0\xB2",
					"\xA3" => "\xD0\xB3",
					"\xA4" => "\xD0\xB4",
					"\xA5" => "\xD0\xB5",
					"\xA6" => "\xD0\xB6",
					"\xA7" => "\xD0\xB7",
					"\xA8" => "\xD0\xB8",
					"\xA9" => "\xD0\xB9",
					"\xAA" => "\xD0\xBA",
					"\xAB" => "\xD0\xBB",
					"\xAC" => "\xD0\xBC",
					"\xAD" => "\xD0\xBD",
					"\xAE" => "\xD0\xBE",
					"\xAF" => "\xD0\xBF",
					"\xB0" => "\xE2\x96\x91",
					"\xB1" => "\xE2\x96\x92",
					"\xB2" => "\xE2\x96\x93",
					"\xB3" => "\xE2\x94\x82",
					"\xB4" => "\xE2\x94\xA4",
					"\xB5" => "\xE2\x95\xA1",
					"\xB6" => "\xE2\x95\xA2",
					"\xB7" => "\xE2\x95\x96",
					"\xB8" => "\xE2\x95\x95",
					"\xB9" => "\xE2\x95\xA3",
					"\xBA" => "\xE2\x95\x91",
					"\xBB" => "\xE2\x95\x97",
					"\xBC" => "\xE2\x95\x9D",
					"\xBD" => "\xE2\x95\x9C",
					"\xBE" => "\xE2\x95\x9B",
					"\xBF" => "\xE2\x94\x90",
					"\xC0" => "\xE2\x94\x94",
					"\xC1" => "\xE2\x94\xB4",
					"\xC2" => "\xE2\x94\xAC",
					"\xC3" => "\xE2\x94\x9C",
					"\xC4" => "\xE2\x94\x80",
					"\xC5" => "\xE2\x94\xBC",
					"\xC6" => "\xE2\x95\x9E",
					"\xC7" => "\xE2\x95\x9F",
					"\xC8" => "\xE2\x95\x9A",
					"\xC9" => "\xE2\x95\x94",
					"\xCA" => "\xE2\x95\xA9",
					"\xCB" => "\xE2\x95\xA6",
					"\xCC" => "\xE2\x95\xA0",
					"\xCD" => "\xE2\x95\x90",
					"\xCE" => "\xE2\x95\xAC",
					"\xCF" => "\xE2\x95\xA7",
					"\xD0" => "\xE2\x95\xA8",
					"\xD1" => "\xE2\x95\xA4",
					"\xD2" => "\xE2\x95\xA5",
					"\xD3" => "\xE2\x95\x99",
					"\xD4" => "\xE2\x95\x98",
					"\xD5" => "\xE2\x95\x92",
					"\xD6" => "\xE2\x95\x93",
					"\xD7" => "\xE2\x95\xAB",
					"\xD8" => "\xE2\x95\xAA",
					"\xD9" => "\xE2\x94\x98",
					"\xDA" => "\xE2\x94\x8C",
					"\xDB" => "\xE2\x96\x88",
					"\xDC" => "\xE2\x96\x84",
					"\xDD" => "\xE2\x96\x8C",
					"\xDE" => "\xE2\x96\x90",
					"\xDF" => "\xE2\x96\x80",
					"\xE0" => "\xD1\x80",
					"\xE1" => "\xD1\x81",
					"\xE2" => "\xD1\x82",
					"\xE3" => "\xD1\x83",
					"\xE4" => "\xD1\x84",
					"\xE5" => "\xD1\x85",
					"\xE6" => "\xD1\x86",
					"\xE7" => "\xD1\x87",
					"\xE8" => "\xD1\x88",
					"\xE9" => "\xD1\x89",
					"\xEA" => "\xD1\x8A",
					"\xEB" => "\xD1\x8B",
					"\xEC" => "\xD1\x8C",
					"\xED" => "\xD1\x8D",
					"\xEE" => "\xD1\x8E",
					"\xEF" => "\xD1\x8F",
					"\xF0" => "\xD0\x81",
					"\xF1" => "\xD1\x91",
					"\xF2" => "\xD0\x84",
					"\xF3" => "\xD1\x94",
					"\xF4" => "\xD0\x87",
					"\xF5" => "\xD1\x97",
					"\xF6" => "\xD0\x8E",
					"\xF7" => "\xD1\x9E",
					"\xF8" => "\xC2\xB0",
					"\xF9" => "\xE2\x88\x99",
					"\xFA" => "\xC2\xB7",
					"\xFB" => "\xE2\x88\x9A",
					"\xFC" => "\xE2\x84\x96",
					"\xFD" => "\xC2\xA4",
					"\xFE" => "\xE2\x96\xA0",
					"\xFF" => "\xC2\xA0");
		
		$outStr = '';
    	for ($i = 0, $len = strlen($string); $i < $len; $i++)
    	{
    		$outStr .= (array_key_exists($string{$i}, $mapping))?$mapping[$string{$i}]:$string{$i};
		}
		
		return $outStr;
	}

?>